package com.binaryTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class LargestValueinEachTreeRow0210_515{
    public List<Integer> largestValues(TreeNode root) {
        if(root == null) return new ArrayList<>();
        Queue<TreeNode> que = new LinkedList<>();
        List<Integer> res = new ArrayList<>();
        que.offer(root);
        while (!que.isEmpty()){
            int len = que.size();
            int min = Integer.MIN_VALUE;
            while (len > 0) {
                TreeNode node = que.poll();
                min = Math.max(node.val, min);
                if (node.left != null) que.offer(node.left);
                if (node.right != null) que.offer(node.right);
                len--;
            }
            res.add(min);
        }
        return res;
    }

}
